package com.shey.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.shey.qiuckpictest.R;
import com.shey.utils.SystemBarTintManager;

/**
 * Created by SheyChen on 2015/12/2 19:11.
 * Email 1768037936@qq.com
 */
public abstract class BaseActivity extends AppCompatActivity {

    public Toolbar mToolBar = null;
    public Toast mToast = null;
    public Context mContext = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        onPreContentView();
        if (getContentViewId() <= 0) {
            throw new IllegalArgumentException("the contentview layout id must be setted");
        }
        setContentView(getContentViewId());
        View decorView = findViewById(android.R.id.content);
        onPostContentView();
        onCreate(savedInstanceState, decorView);
    }

    /**
     * 设置ContentView前
     */
    public void onPreContentView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //状态栏透明，需要在创建SystemBarTintManager之前调用
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        //设置状态栏与ToolBar的颜色保持一致
        tintManager.setStatusBarTintResource(R.color.colorPrimary);
    }

    private void setTranslucentStatus(boolean on) {
        Window window = getWindow();
        WindowManager.LayoutParams winParams = window.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        window.setAttributes(winParams);
    }

    /**
     * 设置ContentView后
     */
    public void onPostContentView() {
        mContext = this;
        mToolBar = (Toolbar) findViewById(R.id.toolbar);
        findView();
        setSupportActionBar(mToolBar);
    }

    public void enabledBackButton() {
        mToolBar.setNavigationIcon(R.mipmap.cmcm_back);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void toast(String toastString) {
        if (mToast == null) {
            mToast = Toast.makeText(getApplicationContext(), toastString, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(toastString);
        }
        mToast.show();
    }

    /**
     * 跳转Activity，无数据参数
     *
     * @param classObj,isClose
     */
    protected void forward(Class<?> classObj, boolean isFinish) {
        Intent intent = new Intent();
        intent.setClass(this, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.startActivity(intent);
        if (isFinish)
            this.finish();
    }

    /**
     * 跳转Activity，有数据参数
     *
     * @param classObj
     * @param params
     * @param isFinish
     */
    protected void forward(Class<?> classObj, Bundle params, boolean isFinish) {
        Intent intent = new Intent();
        intent.setClass(this, classObj);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtras(params);
        this.startActivity(intent);
        if (isFinish)
            this.finish();
    }

    public abstract void onCreate(Bundle savedInstanceState, View decorView);

    public abstract int getContentViewId();

    public abstract void findView();
}
